package com.tmall.testgen.plugin.wizards.runinspring;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;

import com.tmall.testgen.client.ClientDataCenter;
import com.tmall.testgen.client.config.TargetProjectConfig;
import com.tmall.testgen.client.runinspring.RunInSpringHandler;
import com.tmall.testgen.plugin.ConsoleFactory;
import com.tmall.testgen.plugin.wizards.GenTestCodeWizadPage;
import com.tmall.testgen.plugin.wizards.MyMessageDialog;

public class RunInSpingWizad extends Wizard implements INewWizard {
	private RunInSpringWizadPage page;
	private GenTestCodeWizadPage genTestCodePage;
	private ISelection selection;
	private Method testMethod;

	private String targetProjectPath;

	/**
	 * Constructor for CreateTestCodeWizad.
	 */
	public RunInSpingWizad(String targetProjectPath, Method testMethod) {
		super();
		this.testMethod = testMethod;
		this.targetProjectPath = targetProjectPath;
		setNeedsProgressMonitor(true);
		setWindowTitle("本地单元测试");
	}

	public void updateDialog(Method testMethod) {
		this.testMethod = testMethod;
		page.updateDialog(testMethod);
	}

	/**
	 * Adding the page to the wizard.
	 */

	public void addPages() {
		page = new RunInSpringWizadPage(targetProjectPath, testMethod);
		addPage(page);
		genTestCodePage = new GenTestCodeWizadPage(null);
		addPage(genTestCodePage);
	}

	public boolean performFinish() {
		// try {
		// return genTestCodePage.beginGenCode(true);
		// } catch (Exception e) {
		// MyMessageDialog.openExceptionDialog(getShell(), e);
		// return false;
		// }
		IRunnableWithProgress op = new IRunnableWithProgress() {
			public void run(final IProgressMonitor monitor)
					throws InvocationTargetException {
				final List<Exception> exList = new ArrayList<Exception>();
				try {
					monitor.beginTask("gen test code ", 2);
					Display.getDefault().syncExec(new Runnable() {

						@Override
						public void run() {
							try {
								monitor.worked(1);
								boolean bool = genTestCodePage.beginGenCode(true);
								if(!bool){
									exList.add(new Exception("生成代码失败！"));
								}
								monitor.worked(1);
							} catch (Exception e) {
								MyMessageDialog.openExceptionDialog(getShell(),
										e);
								exList.add(e);
							}
						}
					});
				} catch (Exception e) {
					throw new InvocationTargetException(e);
				} finally {
					monitor.done();
				}
				if(exList.size()>0){
					throw new InvocationTargetException(exList.get(0));
				}
			}
		};
		try {
			getContainer().run(true, false, op);
		} catch (Exception e) {
			ConsoleFactory.putError(e);
			MyMessageDialog.openWarnDialog(this.getShell(), "生成代码错误，请查看控制台！");
			return false;
		}
		return true;
	}

	public boolean performCancel() {
		TargetProjectConfig.clear();
		ClientDataCenter.clear();
		RunInSpringHandler.getInstance().clear();
		return super.performCancel();
	}

	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.selection = selection;
	}

}